package com.spring.showcase.interceptors;

import java.util.concurrent.Callable;

import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.async.CallableProcessingInterceptorAdapter;

public class CallableInterceptor extends CallableProcessingInterceptorAdapter {

    @Override
    public <T> void preProcess(NativeWebRequest request, Callable<T> task) throws Exception {
        long startTime = System.currentTimeMillis();
        request.setAttribute("startTime", startTime, RequestAttributes.SCOPE_REQUEST);
    }

    @Override
    public <T> void postProcess(NativeWebRequest request, Callable<T> task, Object concurrentResult) throws Exception {
        long startTime = (Long) request.getAttribute("startTime", RequestAttributes.SCOPE_REQUEST);
        System.out.println("execution took: " + (System.currentTimeMillis() - startTime) + " ms");
    }
}
